const getMysql = require('../mysql')


// 查询首页{带图}的推荐
const trickHomeImg = async(req, res) => {
    let sql = 'select * from ticket_home_img'
    getMysql(sql).then(result => {
        res.send({
            code: 200,
            message: '查询成功！！',
            data: result
        })
    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 首页推荐机票{标题}查询
const trickHomeTitle = async(req, res) => {
    let sql = 'select distinct address from ticket_home_recom'
    getMysql(sql).then(result => {
        res.send({
            code: 200,
            message: '查询成功！！',
            data: result
        })
    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 首页推荐机票地点价格查询
const trickHomeRecom = async(req, res) => {
    console.log(req.body.address);
    let address = req.body.address
    let sql = 'select * from ticket_home_recom where address = ?'
    getMysql(sql, address).then(result => {
        res.send({
            code: 200,
            message: '查询成功！！',
            data: result
        })
    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 通过地点跳转详情页机票查询以及分页
const trickHomeAirpost = async(req, res) => {
    console.log(req.body.address);
    let address = req.body.address;
    let pno = Number((req.body.mid - 1) * 12)
    let sql = 'select * from ticket_home_airport where address = ? limit ?,12'
    let sql2 = `select count(*) as num from ticket_home_airport where address  = ?`
    getMysql(sql, address,pno).then(result => {
        getMysql(sql2, address).then(count => {
            res.send({
                num:count,
                pages:Math.ceil(Number(count[0].num)/12),
                code: 200,
                message: '查询成功！！',
                data: result
            })
        })

    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 通过筛选机舱大小
const trickAirpostVo = async(req, res) => {
    let address = req.body.address
    let lable = "%" + req.body.lable + "%"
    let sql = 'select * from ticket_home_airport where lable like ? and address = ? '
    getMysql(sql, lable, address).then(result => {
        if (result.length == 0) {
            res.send({
                code: 203,
                message: '查不到任何相关数据',
            })
        } else {
            res.send({
                code: 200,
                message: '查询成功！！',
                data: result
            })
        }

    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 通过航空公司进行筛选
const trickSelectAirpost = async(req, res) => {
    let address = req.body.address
    let nickname = "%" + req.body.nickname + "%"
    console.log(address, nickname);
    let sql = 'select * from ticket_home_airport where nickname like ? and address = ? '
    getMysql(sql, nickname, address).then(result => {
        if (result.length == 0) {
            res.send({
                code: 203,
                message: '查不到任何相关数据',
            })
        } else {
            res.send({
                code: 200,
                message: '查询成功！！',
                data: result
            })
        }

    }).catch(err => {
        res.send({
            code: 201,
            message: '查询失败'
        })
    })
}

// 提交信息
const submitInfo = async(req,res)=>{
    let {name,idCard,iphone,startPlace,startTime,username,endTime,longTime,startAirpost,price} = req.body
    let sql = 'insert into trick_collect(name,idCard,iphone,startPlace,startTime,username,endTime,longTime,startAirpost,price) values (?,?,?,?,?,?,?,?,?,?)'
    getMysql(sql, name,idCard,iphone,startPlace,startTime,username,endTime,longTime,startAirpost,price).then(result => {
        res.send({
            coded: 200,
            message: '插入成功！！！',
            // data:result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            coded: 201,
            message: '插入失败',
        })
    })

}



module.exports = {
    trickHomeImg,
    trickHomeTitle,
    trickHomeRecom,
    trickHomeAirpost,
    trickAirpostVo,
    trickSelectAirpost,
    submitInfo
}